<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-满就送折扣
// 代码日期：v3.0 2018-7-14
// 作者：杨道泉
// 文件名：QnuiMjs1Controller.class.php
// 功能： 使用千牛UI发布满就送折扣

class QnuiMjsSaveController extends QnuiBaseController {
    public function save_mjs(){
        $dbId=I("db_id");
        $data["activity_id"]=I("activity_id");
        $data['name']	=I("name");
        $data['type']	=I("type");
        $data['participate_range']	=I("participate_range");
        $data['start_time']	=I("start_time");
        $data['end_time']	=I("end_time");
        $data['is_amount_over']	=I("is_amount_over");
        $data['total_price']	=I("total_price");
        $data['is_amount_multiple']	=I("is_amount_multiple");
        $data['is_item_count_over']	=I("is_item_count_over");
        $data['item_count']	=I("item_count");
        $data['is_item_multiple']	=I("is_item_multiple");
        $data['is_shop_member']	=I("is_shop_member");
        $data['shop_member_level']	=I("shop_member_level");
        $data['is_user_tag']	=I("is_user_tag");
        $data['user_tag']	=I("user_tag");
        $data['is_decrease_money']	=I("is_decrease_money");
        $data['decrease_amount']	=I("decrease_amount");
        $data['is_discount']	=I("is_discount");
        $data['discount_rate']	=I("discount_rate");
        $data['is_send_gift']	=I("is_send_gift");
        $data['gift_name']	=I("gift_name");
        $data['gift_id']	=I("gift_id");
        $data['gift_url']	=I("gift_url");
        $data['gift_pic_url'] = I("gift_pic_url");
        $data['is_free_post']	=I("is_free_post");
        $data['exclude_area']	=I("exclude_area");
        $data["sid"]=session("SHOP_ID");
            
        try{
            $PROM = M("PromotionsMjs");
            if($dbId){
                $id = $PROM->where(Array("id"=>$dbId))->save($data);
            }else{
                $id = $PROM->add($data);
                }
        }catch(\Exception $e) {
            $msg = $e->getMessage();
          }
        if($id){
            $re = new \StdClass();
            $re->code=200;
            $re->id=$id;
        }else{
            $re = new \StdClass();
            $re->code=910;
            $re->msg=$msg;
        }
        $this->ajaxReturn($re);
    }
    
    public function save_items(){
        $items = I("join_items");
        $activity_id = I("activity_id");
        $data = Array();
        foreach($items as $item){
            $data[] = Array("title"=>$item["title"],
                "pic_url"=>$item["pic_url"],
                "num_iid"=>$item["num_iid"],
                "activity_id"=>$activity_id,
                "sid"=>session("SHOP_ID"));
          }
        try{
            $ITEM = M("PromotionsMjsItems");
            $re = $ITEM->addAll($data);
        }catch(\Exception $e) {
            $msg = $e->getMessage();
          }
        if($msg){
            $re = new \StdClass();
            $re->code=910;
            $re->msg=$msg;
        }else{
            $re = new \StdClass();
            $re->code=200;
            $re->id=$id;
          }
//         $re->sql = $ITEM->_sql();
//         $re->data=$data;
//         $re->err = $ITEM->getDbError();
        $this->ajaxReturn($re);
    }
    public function get_mjs_list(){
            $page = I("page");
            $page_size=I("page_size");
            $participate_range=I("participate_range");
        try{
            $where = Array("sid"=>session("SHOP_ID"),"participate_range"=>$participate_range);
            $PROM = M("PromotionsMjs");
            $total = $PROM->where($where)->count();
            $data = $PROM->where($where)
                         ->order("id DESC")
                         ->limit(($page-1)*$page_size,$page_size)
                         ->getField("id,activity_id,
                                        name,
                                        participate_range,
                                        start_time,
                                        end_time,
                                        total_price,
                                        item_count,
                                        is_amount_over,
                                        is_item_count_over,
                                        is_send_gift,
                                        is_decrease_money,
                                        is_discount,
                                        is_free_post,
                                        decrease_amount,
                                        discount_rate,
                                        is_shop_member

                                        ");
        }catch(\Exception $e) {
            $msg = $e->getMessage();
          } 
        if($msg){
            $re = new \StdClass();
            $re->code=910;
            $re->msg=$msg;
        }else{
            $re = new \StdClass();
            $re->code=200;
            if(count($data)==0){
                $data = Array();
            }else{
                $data = array_values($data);
                }
            $re->data=$data;
            $re->total = $total;
          }
        $this->ajaxReturn($re);
    }
    public function get_mjs(){
        $dbId = I("id");
        $PROM = M("PromotionsMjs");
        $data =     $PROM->where(Array("id"=>$dbId,"sid"=>session("SHOP_ID")))
                        ->getField("id,
                                    activity_id,
                                    name,
                                    type,
                                    participate_range,
                                    start_time,
                                    end_time,
                                    is_amount_over,
                                    total_price,
                                    is_amount_multiple,
                                    is_item_count_over,
                                    item_count,
                                    is_item_multiple,
                                    is_shop_member,
                                    shop_member_level,
                                    is_user_tag,
                                    user_tag,
                                    is_decrease_money,
                                    decrease_amount,
                                    is_discount,
                                    discount_rate,
                                    is_send_gift,
                                    gift_name,
                                    gift_id,
                                    gift_url,
                                    gift_pic_url,
                                    is_free_post,
                                    exclude_area");
        foreach($data as $key=>$d){
            $data[$key]["decrease_amount"] = $d["decrease_amount"]/100;
            $data[$key]["discount_rate"] = $d["discount_rate"]/100;
            $data[$key]["total_price"] = $d["total_price"]/100;
            $data[$key]["is_item_count_over"] = $d["is_item_count_over"]=="true"?true:false;
            $data[$key]["is_amount_over"] = $d["is_amount_over"]=="true"?true:false;
            $data[$key]["is_decrease_money"] = $d["is_decrease_money"]=="true"?true:false;
            $data[$key]["is_discount"] = $d["is_discount"]=="true"?true:false;
            if($d["is_send_gift"]=="true"){
                $gift["pic_url"]=$d["gift_pic_url"];
                $gift["title"]=$d["gift_name"];
                $gift["num_iid"]=$d["gift_id"];
                $data[$key]["gift"][]=$gift;
            }else{
                $data[$key]["gift"]=false;
                }
            if($d["exclude_area"]!=""){
                $area = explode("#",$d["exclude_area"]);
                $area_code=explode("*",$area[0]);
                $area_name=explode("*",$area[1]);
                $exclude_area = Array();
                foreach($area_code as $k=>$code){
                    $exclude_area[]=Array("id"=>$code,"name"=>$area_name[$k]);
                }
            }
            $data[$key]["exclude_area"]=$exclude_area;
        }
        $re = new \StdClass();
        $re->code=200; 
        $re->data=current($data);
      $this->ajaxReturn($re);
    }
    public function remove_mjs(){
        $dbId = I("id");
        $activity_id = I("activity_id");
        try{
            $PROM = M("PromotionsMjs");
            $PROM->where(Array("id"=>$dbId,"sid"=>session("SHOP_ID"),"activity_id"=>$activity_id))
                 ->delete();
        }catch(\Exception $e) {
            $msg = $e->getMessage();
           } 
        if($msg){
           $re = new \StdClass();
           $re->code=910;
           $re->msg=$msg;
        }else{
           $re = new \StdClass();
           $re->code=200;
           }
        $this->ajaxReturn($re);
    }
}